package org.micropayment.consumer;

import java.util.List;
import org.micropayment.model.MicropaymentUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.micropayment.model.data.ProviderData;
import org.micropayment.model.ProviderResource;
import org.micropayment.model.service.ProviderResourceDAO;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * TODO: improve this!!!
 * @author jj
 */
public class AppConfigurer extends HttpServlet {

    private static Logger logger =
        LoggerFactory.getLogger(AppConfigurer.class);

    private boolean dataInitializated = false;

    @Override
    public void init() throws ServletException {
        WebApplicationContext springContext =
            WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        ProviderResourceDAO providerDao =
                (ProviderResourceDAO)springContext.getBean("providerDao");
        ProviderResource provider = providerDao.findById("InternetBank");
        if (provider != null) dataInitializated = true;
    }

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        if (!dataInitializated) {
            //Database not initializated
            logger.debug("########## AppConfigurer - inserting data ##############");
            insertProviders();
            dataInitializated = true;
        }
        response.sendRedirect(getServletContext().getContextPath()
                + "/transactionPoster.jsp");
    }

    private void insertProviders () {
        WebApplicationContext springContext =
                WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        ProviderResourceDAO providerDao =
                    (ProviderResourceDAO)springContext.getBean("providerDao");
        providerDao.save(ProviderData.getProviderResource());
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Application configurer";
    }// </editor-fold>

}
